package com.buddy.learn.algorithm.demo;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;

/**
 * @author Buddy
 * @date 2021/5/6
 */
public class _013句子反转 {

    /**
     * 将一个英文语句以单词为单位逆序排放。例如“I am a boy”，逆序排放后为“boy a am I”
     * 所有单词之间用一个空格隔开，语句中除了英文字母外，不再包含其他字符
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Deque<String> words = new LinkedList<>();
        String[] strs = scanner.nextLine().split("\\s+");
        for (String word : strs) {
            words.addFirst(word);
        }
        System.out.println(String.join(" ", words));
    }

    public static String getResult(String input) {
        StringBuffer sb = new StringBuffer();
        Stack<String> stack = new Stack<>();
        String[] arr = input.split("\\s");
        for (String str : arr) {
            stack.push(str);
        }
        while (!stack.isEmpty()) {
            sb.append(stack.pop() + " ");
        }
        return sb.toString();
    }
}
